package com.iss.okair.service.assets;

import com.iss.common.StringTools;
import com.iss.common.UuidTools;
import com.iss.okair.db.entity.AssetsNormal;
import com.iss.okair.db.entity.Category;
import com.iss.okair.db.mapper.AssetsNornalMapper;
import com.iss.okair.db.mapper.CategoryMapper;
import com.iss.okair.db.model.AssetsNormalMode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AssetsNormalService {
    @Resource
    private AssetsNornalMapper mapper;

    @Resource
    private CategoryMapper cmapper;

    public List<AssetsNormal> find(AssetsNormalMode model){
        return mapper.select(model);
    }
//保存
    public boolean save(AssetsNormal assets) {
        if (StringTools.isNullOrEmpty(assets.getId())) {
            //添加
            //设置id值
            assets.setId(UuidTools.buildUUID());
            //调用私有方法，生成编码
            assets.setAssetscode(this.buildAssetsCode(assets.getAssetstype()));
            return mapper.add(assets)>=1?true:false;

        }
        else{
            //修改
            return mapper.update(assets)>=1?true:false;
        }
    }
    private String buildAssetsCode(int categoryId){
        Category currentCategory = cmapper.selectById(categoryId);

        String temp = String.format("NOR%s-%06d",currentCategory.getCode(),mapper.countALL());

        return temp;
    }
}
